package com.yswy.backstage.service.impl;

import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yswy.backstage.dto.req.TgChannleListVo;
import com.yswy.backstage.entity.user.User;
import com.yswy.backstage.service.PackageManageService;
import com.yswy.backstage.service.TgChannleChildService;
import com.yswy.backstage.service.TgChannleService;
import com.yswy.common.utils.HttpUtil;
import com.yswy.common.utils.StringUtils;
import com.yswy.domain.common.BasePojo;
import com.yswy.domain.entity.TgChannle;
import com.yswy.domain.entity.TgChannleChild;
import com.yswy.domain.mapper.TgChannleChildMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class TgChannleChildServiceImpl extends ServiceImpl<TgChannleChildMapper, TgChannleChild>
        implements TgChannleChildService {
    static String spileCode = "-";

    @Value("${zjym}")
    private String zjym;

    @Resource
    private TgChannleService tgChannleService ;
    @Override
    public Page<TgChannleChild> listTgChannleChild(TgChannleListVo tgChannleListVo) {
        Page<TgChannleChild> page = page(new Page<>(tgChannleListVo.getPageNum(), tgChannleListVo.getPageSize()),
                new LambdaQueryWrapper<TgChannleChild>()
                        .eq(TgChannleChild::getIsDel , 0)
                        .eq(TgChannleChild::getTgChannleId , tgChannleListVo.getTgChannleId())
                        .like(StringUtils.isNotEmpty(tgChannleListVo.getChannle()) , TgChannleChild::getChannle , tgChannleListVo.getChannle())
                        .ge(tgChannleListVo.getStaTime()!= null ,TgChannleChild::getCreateTime , tgChannleListVo.getStaTime())
                        .le(tgChannleListVo.getEndTime()!= null ,TgChannleChild::getCreateTime , tgChannleListVo.getEndTime())
        );

        List<TgChannleChild> tgChannleChildList = page.getRecords();
        for (TgChannleChild tgChannleChild :tgChannleChildList){
            tgChannleChild.setExaminePrice(tgChannleChild.getExaminePrice()/100);
        }
        page.setRecords(tgChannleChildList);
        return page;
    }

    @Override
    public void addOrUpdateTgChannleChild(TgChannleChild tgChannleChild, User user) {


        tgChannleChild.setExaminePrice(tgChannleChild.getExaminePrice()*100);

        TgChannle tgChannle = tgChannleService.getById(tgChannleChild.getTgChannleId());
        if (!tgChannleChild.getChannle().contains(tgChannle.getChannle()+spileCode)){
            tgChannleChild.setChannle(tgChannle.getChannle()+spileCode+tgChannleChild.getChannle());
        }

        if(tgChannleChild.getId() != null){
            tgChannleChild.setCreateBy(user.getId().longValue());
            tgChannleChild.setCreateTime(new Date());

        }else {
            Integer integer = count(new LambdaQueryWrapper<TgChannleChild>().eq(TgChannleChild::getChannle ,tgChannleChild.getChannle()).eq(BasePojo::getIsDel,0));
            if (integer>0){
                throw new IllegalStateException("渠道已存在");
            }
            tgChannleChild.setUpdateBy(user.getId().longValue());
            tgChannleChild.setUpdateTime(new Date());
        }

        String urlMsg = zjym+"tg/handleCallback?channle="+ tgChannleChild.getChannle()+
                "&type=2&ADVERTISER_ID=__ADVERTISER_ID__&CSITE=__CSITE__&CONVERT_ID=__CONVERT_ID__&IMEI=__IMEI__&ANDROIDID=__ANDROIDID__&OAID=__OAID__&CALLBACK_PARAM=__CALLBACK_PARAM__&OS=__OS__&MAC=__MAC__&IP=__IP__&MODEL=__MODEL__";
        tgChannleChild.setUrlMsg(urlMsg);

        saveOrUpdate(tgChannleChild);
    }

    @Override
    public void delTgChannleChild(Long Id, User user) {
        TgChannleChild tgChannleChild = getById(Id);
        tgChannleChild.setIsDel(1);
        tgChannleChild.setUpdateBy(user.getId().longValue());
        tgChannleChild.setUpdateTime(new Date());
        updateById(tgChannleChild);
    }
}
